package android.slc.mp.ui.page.file;

import static android.provider.MediaStore.MediaColumns.DATE_MODIFIED;
import static android.provider.MediaStore.MediaColumns.MIME_TYPE;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.slc.ml.MediaLoader;
import android.slc.ml.callback.file.OnFileLoaderOnBaseCallBack;
import android.slc.ml.utils.MediaLoaderFileUtils;
import android.slc.ml.utils.MediaLoaderUriUtils;
import android.slc.mp.R;
import android.slc.mp.SlcMp;
import android.slc.mp.po.FileFolder;
import android.slc.mp.po.FileItem;
import android.slc.mp.po.FileResult;
import android.slc.mp.po.i.IFileFolder;
import android.slc.mp.po.i.IFileItem;
import android.slc.mp.po.i.IFileResult;
import android.slc.mp.ui.SlcIMpDelegate;
import android.slc.mp.ui.page.SlcMpPagerBaseVirtualVm;
import android.slc.mp.utils.SlcMpMediaBrowseUtils;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import androidx.fragment.app.FragmentActivity;
import androidx.loader.content.Loader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by slc
 */
public class SlcMpPagerFileVirtualVm extends SlcMpPagerBaseVirtualVm<IFileResult, IFileFolder, IFileItem> {

    //默认的为文件类型
    public SlcMpPagerFileVirtualVm(SlcIMpDelegate mediaPickerDelegate) {
        this(SlcMp.MEDIA_TYPE_FILE, mediaPickerDelegate);
    }

    public SlcMpPagerFileVirtualVm(int mediaType, SlcIMpDelegate mediaPickerDelegate) {
        super(mediaType, mediaPickerDelegate);
    }

    @Override
    public void loader(FragmentActivity fragmentActivity) {
        MediaLoader.getLoader().loadFiles(fragmentActivity,
                new OnFileLoaderOnBaseCallBack<IFileResult>(getMediaPickerDelegate().getFilePropertyWithMediaType(getMediaType())) {
                    @Override
                    protected IFileResult generateResultDoInBackground(Loader<Cursor> loader, Cursor data) {
                        List<IFileFolder> folders = new ArrayList<>();

                        IFileFolder folder, allItemFolder = new FileFolder();

                        if (data != null) {
                            IFileItem item;
                            while (data.moveToNext()) {
                                long fileId = data.getLong(data.getColumnIndexOrThrow(BaseColumns._ID));
                                String path = data.getString(data.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
                                long size = data.getLong(data.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE));
                                String name = data.getString(data.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME));
                                String mime = data.getString(data.getColumnIndexOrThrow(MIME_TYPE));
                                long modified = data.getLong(data.getColumnIndexOrThrow(DATE_MODIFIED));
                                item = new FileItem();
                                item.setMediaTypeTag(getMediaType());
                                item.setId(fileId);
                                item.setPath(path);
                                item.setUri(MediaLoaderUriUtils.id2Uri(MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL), fileId));
                                item.setSize(size);
                                if (TextUtils.isEmpty(name)) {
                                    name = MediaLoaderFileUtils.getFileName(path);
                                }
                                item.setDisplayName(name);
                                modified = MediaLoaderFileUtils.getFileLastModified(path);
                                item.setModified(modified);
                                item.setExtension(MediaLoaderFileUtils.getFileExtension(path));
                                if (TextUtils.isEmpty(mime)) {
                                    mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(item.getExtension());
                                }
                                item.setMime(mime);

                                String parent = path.substring(0, path.lastIndexOf(File.separator));
                                folder = new FileFolder(parent, parent.substring(parent.lastIndexOf(File.separator) + 1));
                                if (folders.contains(folder)) {
                                    folders.get(folders.indexOf(folder)).addItem(item);
                                } else {
                                    folder.addItem(item);
                                    folders.add(folder);
                                }
                                allItemFolder.addItem(item);
                            }
                        }
                        //此处不用添加headItem
                        allItemFolder.setName(SlcMp.getInstance().getApp().getString(R.string.slc_m_p_all_file));
                        folders.add(0, allItemFolder);
                        return new FileResult(folders);
                    }

                    @Override
                    public void onResult(IFileResult result) {
                        SlcMpPagerFileVirtualVm.this.onResult(result);
                    }

                });
    }

    @Override
    public void onItemClick(int position) {
        SlcMpMediaBrowseUtils.openMedia(getMediaPickerDelegate().getContext(), getCurrentMediaList().get(position).getUri(),
                getCurrentMediaList().get(position).getMime());
    }

    @Override
    public void onItemChildClick(int position) {
        SlcMpMediaBrowseUtils.openMedia(getMediaPickerDelegate().getContext(), getCurrentMediaList().get(position).getUri(),
                getCurrentMediaList().get(position).getMime());
    }

}
